package com.abner.juc;

import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author Abner
 */
public class CompletableFutureDemoTest {

    /**
     * 同步
     */
    @Test
    public void test1() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"\t没有返回");
        });
        completableFuture.get();
    }

    /**
     * 异步
     */
    @Test
    public void test2() throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("---");
            int a = 10 / 0;
            return 200;
        });
        System.out.println( completableFuture.whenComplete((t,u) -> {
            System.out.println("成功执行---");
            System.out.println("t"+t);
            System.out.println(u);
        }).exceptionally(f -> {
            System.out.println("异常了----");
            System.out.println(f.getMessage());
            return 400;
        }).get());

    }
}
